cmake_minimum_required(VERSION 2.8)

project(RestApiSample)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -std=c++0x")

file(DOWNLOAD 
  http://mongoose.googlecode.com/files/mongoose-3.1.tgz 
  ${CMAKE_BINARY_DIR}/mongoose-3.1.tar.gz
  EXPECTED_MD5 "e718fc287b4eb1bd523be3fa00942bb0"
  SHOW_PROGRESS
  )

file(DOWNLOAD 
  http://downloads.sourceforge.net/project/jsoncpp/jsoncpp/0.5.0/jsoncpp-src-0.5.0.tar.gz
  ${CMAKE_BINARY_DIR}/jsoncpp-src-0.5.0.tar.gz
  EXPECTED_MD5 "24482b67c1cb17aac1ed1814288a3a8f"
  SHOW_PROGRESS
  )

execute_process(
  COMMAND hg clone -v -r Orthanc-0.4.0 https://code.google.com/p/orthanc/ Orthanc-0.4.0
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  )

execute_process(
  COMMAND ${CMAKE_COMMAND} -E tar xvfz ${CMAKE_BINARY_DIR}/mongoose-3.1.tar.gz
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  )

# Apply a patch to improve Mongoose shutdown
execute_process(
  COMMAND patch mongoose.c ${ORTHANC_DIR}/Resources/Patches/mongoose-patch.diff
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/mongoose
  )

execute_process(
  COMMAND ${CMAKE_COMMAND} -E tar xvfz ${CMAKE_BINARY_DIR}/jsoncpp-src-0.5.0.tar.gz
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  )

include(AutoGeneratedCode.cmake)

add_definitions(
  -DBOOST_HAS_FILESYSTEM_V3=1
  -DBOOST_HAS_SCHED_YIELD=1
  -DORTHANC_SSL_ENABLED=1
  -DORTHANC_STANDALONE=1
  -DORTHANC_STATIC=0
  )

set(ORTHANC_DIR ${CMAKE_BINARY_DIR}/Orthanc-0.4.0)
set(MONGOOSE_DIR ${CMAKE_BINARY_DIR}/mongoose)
set(JSONCPP_DIR ${CMAKE_BINARY_DIR}/jsoncpp-src-0.5.0)

include_directories(
  ${ORTHANC_DIR}
  ${MONGOOSE_DIR}
  ${JSONCPP_DIR}/include
  )

link_libraries(
  boost_date_time
  boost_filesystem
  boost_system
  boost_thread
  curl
  dl
  glog
  png
  pthread
  sqlite3
  uuid
  z
  )

set(THIRD_PARTY_SOURCES
  ${MONGOOSE_DIR}/mongoose.c
  ${JSONCPP_DIR}/src/lib_json/json_reader.cpp
  ${JSONCPP_DIR}/src/lib_json/json_value.cpp 
  ${JSONCPP_DIR}/src/lib_json/json_writer.cpp
  )

file(GLOB ORTHANC_SOURCES 
  ${ORTHANC_DIR}/Core/*.cpp
  ${ORTHANC_DIR}/Core/*/*.cpp
  ${ORTHANC_DIR}/OrthancCppClient/*.cpp
  ${ORTHANC_DIR}/Resources/base64/base64.cpp
  ${ORTHANC_DIR}/Resources/md5/md5.c
  ${ORTHANC_DIR}/Resources/sha1/sha1.cpp
  ${ORTHANC_DIR}/Resources/minizip/zip.c
  ${ORTHANC_DIR}/Resources/minizip/ioapi.c
  )

list(REMOVE_ITEM ORTHANC_SOURCES ${ORTHANC_DIR}/OrthancCppClient/main.cpp)

EmbedResources(
  #ORTHANC_EXPLORER ${ORTHANC_DIR}/OrthancExplorer
  )

add_executable(RestApiSample
  Sample.cpp
  ${ORTHANC_SOURCES}
  ${AUTOGENERATED_SOURCES}
  ${THIRD_PARTY_SOURCES}
  )
